import { getCategoryAPI } from "@/apis/category";
import type { CategoryEJI } from "@/types/category/category";
import { onMounted, ref } from "vue";
import { onBeforeRouteUpdate, useRoute } from "vue-router";

export function useCategory() {
  const route = useRoute();
  const categoryData = ref<CategoryEJI>({} as CategoryEJI);
  const getCategory = async (id = route.params.id) => {
    const res = await getCategoryAPI(id);
    categoryData.value = res.data.result;
  };
  onMounted(() => {
    getCategory();
  });
  //解决路由的缓存问题
  onBeforeRouteUpdate((to) => {
    //存在有问题
    getCategory(to.params.id);
  });

  return {
    categoryData,
  };
}
